package com.proj.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.diagnosis.DiagnosisUtils;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.domain.ExtendParams;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.domain.GoodsDetail;
import com.alipay.api.domain.BusinessParams;
import com.alipay.api.request.AlipayTradePrecreateRequest;

import com.alipay.api.FileItem;
import com.proj.bean.Order;

import java.util.Base64;
import java.util.ArrayList;
import java.util.List;

public class PaymentUtil {


        public static void main1(String[] args) throws AlipayApiException {
            String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCVOJVPrTONXkiAHj42NcOCq2tJs0vThEaLFk4ovN5pzplnY+J4WWyK9kjVhRuFL9QnVPLW3pQAU7uP/y06FBZn2Lg5nFkkuhVbRyXV1UkSIxgMDPsbpvhD+gRJ5hPgOOW6rCIbDtG29f9GktjjGTNZFVthH+d2Rs1/24TVRICxXZZgpFqLQ1Iz1CQs/GKxeFoGysmzGNcvTd1lQ1y4ieAlMhdJtrBteJ7zbc483oVK9R1vKFrbHqmNy+ktEWbxepF9DW79yvpR1Qj1oPZ+hGjikVguAx47xl6mn/166siVs+uAWE7zfvK53vVv8pjUKVfmSnjz7bFR60P4JiN2un2hAgMBAAECggEALzM4t303w03BbRUj5I3ZC+ohA6z9HSjID5K8olIroBhDmdqUdjEDDmmc3D1AWT6pws8yCRlag0f5GjFDIw3bkHmSvFZiNCXQP5EfvKW8eUo0fwtUGokUGpxo4t8uBky5DfUIzLpBR7hESRSGGj+MaG9C+lPXKSh4NCtBtxv2ASP1ovKJpsoa6Dw5Abyrvk0qCGsozBxcP9nDIN4ayR9qVleFt34zDcS/6YP2xNF7BvdF313tNbfjzMBBcdvZ3INGJYnakByqqs3F2RE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pamNv8gm8EA4gIxITcZGmH7rGZuuXmvf1m4KYfKOXCCf5laEtp6/+iGKISvyIXxM3H/89hcSU0deNvO19bGlcyUphJmfQh02uxs+EEm43TvK2K5FedJ8RU7xdNWu3/4gnvQvF0xsK2w6J9Ua40VbriDuOgFj8y02+I1vHOyTT63kxqt613JKzQarH/QxMHf5idU8jRbxL3oGbvkYyH8mGbV0pPZ17BqEOveEzt/L0LPKvFg+VlkHQiYBOHn975tCyF/9K8QBvTIn2/J0qLiQIDAQAB";
            AlipayConfig alipayConfig = new AlipayConfig();
            alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
            alipayConfig.setAppId("9021000146600865");
            alipayConfig.setPrivateKey(privateKey);
            alipayConfig.setFormat("json");
            alipayConfig.setAlipayPublicKey(alipayPublicKey);
            alipayConfig.setCharset("UTF-8");
            alipayConfig.setSignType("RSA2");
            AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
            AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
            AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();
            model.setOutTradeNo("20150320010101001");
            model.setTotalAmount("88.88");
            model.setSubject("Iphone6 16G");
            request.setBizModel(model);
            AlipayTradePrecreateResponse response = alipayClient.execute(request);
//            System.out.println(response.getBody());
            if (response.isSuccess()) {
//                System.out.println("调用成功");
            } else {
//                System.out.println("调用失败");
                // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
                // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
                // System.out.println(diagnosisUrl);
            }
        }


        public static String payment(Order order) throws AlipayApiException {
            // 初始化SDK
            AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());

            // 构造请求参数以调用接口
            AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
            AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();

//            // 设置商户订单号
//            model.setOutTradeNo("20150320010101001");
            model.setOutTradeNo(order.getCode());
//
//            // 设置订单总金额
//            model.setTotalAmount("88.88");
            model.setTotalAmount(order.getMoney().toString());
//
//            // 设置订单标题
//            model.setSubject("Iphone6 16G");
            model.setSubject("乐百惠商城");
//
//            // 设置产品码
            model.setProductCode("FACE_TO_FACE_PAYMENT");
//            model.setProductCode("QR_CODE_OFFLINE");

            // 设置卖家支付宝用户ID
            model.setSellerId("2088721061065954");

            // 设置订单附加信息
//            model.setBody("Iphone6 16G");
//
//            // 设置订单包含的商品列表信息
//            List<GoodsDetail> goodsDetail = new ArrayList<GoodsDetail>();
//            GoodsDetail goodsDetail0 = new GoodsDetail();
//            goodsDetail0.setGoodsName("ipad");
//            goodsDetail0.setQuantity(1L);
//            goodsDetail0.setPrice("2000");
//            goodsDetail0.setGoodsId("apple-01");
//            goodsDetail0.setGoodsCategory("34543238");
//            goodsDetail0.setCategoriesTree("124868003|126232002|126252004");
//            goodsDetail0.setShowUrl("http://www.alipay.com/xxx.jpg");
//            goodsDetail.add(goodsDetail0);
//            model.setGoodsDetail(goodsDetail);

//            // 设置业务扩展参数
//            ExtendParams extendParams = new ExtendParams();
//            extendParams.setSysServiceProviderId("2088511833207846");
//            model.setExtendParams(extendParams);

            // 设置商户传入业务信息
//            BusinessParams businessParams = new BusinessParams();
//            businessParams.setMcCreateTradeIp("127.0.0.1");
//            model.setBusinessParams(businessParams);

            // 设置可打折金额
//            model.setDiscountableAmount("80.00");

            // 设置商户门店编号
//            model.setStoreId("NJ_001");

            // 设置商户操作员编号
//            model.setOperatorId("yx_001");

            // 设置商户机具终端编号
//            model.setTerminalId("NJ_T_001");

            // 设置商户的原始订单号
//            model.setMerchantOrderNo("20161008001");

            request.setBizModel(model);
            //request.setNotifyUrl();//设置一个异步回调的地址
            // /////////////////////////////////////////////////////////////////////////////////////////////////////////
            // /////////////////////////////////////////////////////////////////////////////////////////////////////////
            //
             //
            String REQURL = "https://6786d0bd.r40.cpolar.top";
            //
            //
            request.setNotifyUrl(REQURL+"/order/zhifu?orderid="+order.getId());
            //
            // /////////////////////////////////////////////////////////////////////////////////////////////////////////
            // /////////////////////////////////////////////////////////////////////////////////////////////////////////
            // 第三方代调用模式下请设置app_auth_token
            // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");

            AlipayTradePrecreateResponse response = alipayClient.execute(request);
//            System.out.println(response);
//            System.out.println(response.getBody());


            if (response.isSuccess()) {
                String body = response.getBody();
                JSONObject obj=JSON.parseObject(body);
                obj=obj.getJSONObject("alipay_trade_precreate_response");
                String qr_code=obj.getString("qr_code");

//                System.out.println("调用成功");
                return qr_code;
            } else {
                System.out.println("调用失败");
                System.out.println(order.getCode());
                // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
                 String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
//                 System.out.println(diagnosisUrl);
                 return null;
            }
        }

        private static AlipayConfig getAlipayConfig() {
            String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCVOJVPrTONXkiAHj42NcOCq2tJs0vThEaLFk4ovN5pzplnY+J4WWyK9kjVhRuFL9QnVPLW3pQAU7uP/y06FBZn2Lg5nFkkuhVbRyXV1UkSIxgMDPsbpvhD+gRJ5hPgOOW6rCIbDtG29f9GktjjGTNZFVthH+d2Rs1/24TVRICxXZZgpFqLQ1Iz1CQs/GKxeFoGysmzGNcvTd1lQ1y4ieAlMhdJtrBteJ7zbc483oVK9R1vKFrbHqmNy+ktEWbxepF9DW79yvpR1Qj1oPZ+hGjikVguAx47xl6mn/166siVs+uAWE7zfvK53vVv8pjUKVfmSnjz7bFR60P4JiN2un2hAgMBAAECggEALzM4t303w03BbRUj5I3ZC+ohA6z9HSjID5K8olIroBhDmdqUdjEDDmmc3D1AWT6pws8yCRlag0f5GjFDIw3bkHmSvFZiNCXQP5EfvKW8eUo0fwtUGokUGpxo4t8uBky5DfUIzLpBR7hESRSGGj+MaG9C+lPXKSh4NCtBtxv2ASP1ovKJpsoa6Dw5Abyrvk0qCGs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QEAr4p6U9vtsK97Ak32F0ee0+XcR2ZVqhGYSsOiLOkBIUGnPC7OcOlsIFA9PzAaGCtie7pamNv8gm8EA4gIxITcZGmH7rGZuuXmvf1m4KYfKOXCCf5laEtp6/+iGKISvyIXxM3H/89hcSU0deNvO19bGlcyUphJmfQh02uxs+EEm43TvK2K5FedJ8RU7xdNWu3/4gnvQvF0xsK2w6J9Ua40VbriDuOgFj8y02+I1vHOyTT63kxqt613JKzQarH/QxMHf5idU8jRbxL3oGbvkYyH8mGbV0pPZ17BqEOveEzt/L0LPKvFg+VlkHQiYBOHn975tCyF/9K8QBvTIn2/J0qLiQIDAQAB";
            AlipayConfig alipayConfig = new AlipayConfig();
            alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
            alipayConfig.setAppId("9021000146600865");
            alipayConfig.setPrivateKey(privateKey);
            alipayConfig.setFormat("json");
            alipayConfig.setAlipayPublicKey(alipayPublicKey);
            alipayConfig.setCharset("UTF-8");
            alipayConfig.setSignType("RSA2");
            return alipayConfig;
        }
    }

